/**
 * 
 */
package com.leoj.utils.tcp_network.tcp_server;

import java.io.IOException;
import java.net.Socket;

import com.leoj.utils.tcp_network.ApplicationProtocolFactory;
import com.leoj.utils.tcp_network.TcpConnection;
import com.leoj.utils.tcp_network.TcpServerSocketThread;

/**
 * @author leoj
 *
 */
public class MultiThreadTcpServer extends TcpServerSocketThread {

	/**
	 * @param port
	 * @param timeout
	 * @throws IOException
	 */
	public MultiThreadTcpServer(int port, int timeout, ApplicationProtocolFactory factory) throws IOException {
		super(port, timeout, factory);
	}
	public MultiThreadTcpServer(int port, ApplicationProtocolFactory factory) throws IOException {
		super(port, factory);
	}
	/* (non-Javadoc)
	 * @see com.leoj.utils.network.TcpServerSocketThread#handleNewConnection(java.net.Socket)
	 */
	@Override
	protected void handleNewConnection(final Socket s) {
		System.out.println("new TCP connection on the MultiThreadTcpServer !");
		System.out.println("tcp server @"+this.getServerStrAddress()+":"+this.getServerPort());
		new Thread(){
			/* (non-Javadoc)
			 * @see java.lang.Thread#run()
			 */
			@Override
			public void run() {
				TcpConnection connection = null; 
				try {
					connection = new TcpConnection(s);
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
				_protocolFactory.createProtocol().handleClient(connection);
			}
		}.start();

	}

}
